﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using MySql.Data.MySqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MyDns
{
    public partial class Form1 : Form
    {
        string iniPath = Application.StartupPath + "\\mysqlconn.ini";
        public Form1()
        {
            InitializeComponent();
        }
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);
        private void btn_dnsWrite_Click(object sender, EventArgs e)
        {
            //dns.ServerName = "192.168.1.110";
          //  dns.userName = "Administrator";
            //dns.passWord = "a62411570";
            string domain = txtDomain.Text;
            StringBuilder server_domain = new StringBuilder(20);
            StringBuilder server_ip = new StringBuilder(20);
            string ip = txtIP.Text;
            if (domain != "" && ip != "")
            {
                string OwnerName = "";
                string hostname="";//设备mac read[1]
                string selfdomain = "";//用户自定义域名 read[2]
                string ipAddr="";      //设备的IP  read[3]
                foreach (DataGridViewRow row in dgv.Rows)
                {

                    hostname = row.Cells[1].Value.ToString();
                    selfdomain = row.Cells[2].Value.ToString();
                    ipAddr=row.Cells[3].Value.ToString();
                    hiednsLib.DnsModifyClass dns = new hiednsLib.DnsModifyClass();
                    GetPrivateProfileString("server", "server_domain", "", server_domain, 20, iniPath);
                    GetPrivateProfileString("server", "server_ip", "", server_ip, 20, iniPath);
                    if (hostname==selfdomain)
                    {
                        OwnerName = hostname + "." + domain;
                    }
                    else
                    {
                        OwnerName = selfdomain + "." + domain;
                    }
                    dns.SetDnsSvr(server_domain.ToString(), server_ip.ToString(), 0);
                    int ret = dns.Hie_DnsModify(hostname, selfdomain, ipAddr, 0);
                    if (ret != 0)
                    {
                        StreamWriter sw = new StreamWriter(Application.StartupPath + "\\error.txt", false, System.Text.Encoding.Default);
                        sw.WriteLine("错误号:"+ret.ToString()+"   "+hostname + "   " + selfdomain + "    " + ipAddr);
                        sw.Flush();
                        sw.Close();
                        MessageBox.Show(ret.ToString());
                    }
                    //不存一条记录,就写入
                }
            }
        }

        private void btn_DnsRead_Click(object sender, EventArgs e)
        {
            DataTable table = new DataTable();
            table.Columns.Add("index");
            table.Columns.Add("mac");
            table.Columns.Add("domain");
            table.Columns.Add("ip");
            int index = 0;
            MySqlDBHelper db = new MySqlDBHelper();
            string strSql = "select *from dns_info";
            var cmd=db.GetSqlStringCommond(strSql) as MySqlCommand;
            var read=db.ExecuteReader(cmd) as MySqlDataReader;
            while (read.Read())
            {
                DataRow row = table.NewRow();
                row["index"] = ++index;
                row["mac"] = read.GetString("dns_mac");
                row["domain"] = read.GetString("dns_name");
                row["ip"] = read.GetString("dns_ip");
                table.Rows.Add(row);
            }
            dgv.AutoGenerateColumns = false;
            dgv.Columns[0].DataPropertyName = "index";
            dgv.Columns[1].DataPropertyName = "mac";
            dgv.Columns[2].DataPropertyName = "domain";
            dgv.Columns[3].DataPropertyName = "ip";
            dgv.DataSource = table;
        }
    }
}
